package org.rcloud.io.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.rcloud.io.Utils.PageVo;
import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.model.ArticleTable010Mapping;
import org.rcloud.io.model.Travel;
import org.rcloud.io.model.TravelSite;
import org.rcloud.io.service.TravelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/3/12.
 */
@RestController
@RequestMapping(value = "/travel")
public class TravelController {

    @Autowired
    private TravelService travelService;

    /*
  * 获取所有的旅游服务信息
  * */
    @ApiOperation(value="获取所有的旅游服务信息", notes="获取所有的旅游服务信息")
    @ResponseBody
    @RequestMapping(value = "/findTravel", produces = {"application/json;charset=UTF-8"})
    public PageVo<Travel> findTravel(){
        return travelService.findTravel();
    }

    /*
   * 根据eid获取旅游服务信息
   * */
    @ApiOperation(value="根据eid获取旅游服务信息", notes="根据eid获取旅游服务信息")
    @ResponseBody
    @RequestMapping(value = "/findByEid", produces = {"application/json;charset=UTF-8"})
    public ResultVo findByEid(@RequestBody String params){
        JSONObject object= JSON.parseObject(params);
        return travelService.findByEid(object.getString("eid"));
    }

    /*
  * 根据id获取风景线路信息
  * */
    @ApiOperation(value="根据id获取风景线路信息", notes="根据id获取风景线路信息")
    @ResponseBody
    @RequestMapping(value = "/findById", produces = {"application/json;charset=UTF-8"})
    public ResultVo findById(@RequestBody String params){
        String[] ids = params.split("=");
        int id=Integer.parseInt(ids[1]);
        return travelService.findById(id);
    }

    /*
  * 保存旅游服务信息
  * */
    @ApiOperation(value="保存旅游服务信息", notes="保存旅游服务信息")
    @ResponseBody
    @RequestMapping(value = "/saveTravel", produces = {"application/json;charset=UTF-8"})
    public ResultVo saveTravel(@RequestBody String params) {

        JSONObject object = JSON.parseObject(params);

        Map map = JSON.parseObject(object.toString());
        String eid = map.get("eid").toString();
        String name = map.get("name").toString();
        String address = map.get("address").toString();
        String longitude = map.get("longitude").toString();
        String latitude = map.get("latitude").toString();
        String etype = map.get("etype").toString();
        String summary = map.get("summary").toString();
        String report = map.get("report").toString();
        String username = map.get("username").toString();
        String userphone = map.get("userphone").toString();

        Travel travel = new Travel();
        travel.setEid(eid);
        travel.setName(name);
        travel.setAddress(address);
        travel.setLongitude(longitude);
        travel.setLatitude(latitude);
        travel.setEtype(etype);
        travel.setSummary(summary);
        travel.setReport(report);
        travel.setUsername(username);
        travel.setUserphone(userphone);
        travel.setFlag(1);

        return travelService.savetravel(travel);
    }
          /*
           * 修改旅游服务信息
           * */
    @ApiOperation(value="修改旅游服务信息", notes="修改旅游服务信息")
    @ResponseBody
    @RequestMapping(value = "/updateTravel", produces = {"application/json;charset=UTF-8"})
    public ResultVo updateTravel(@RequestBody String params) {
        JSONObject object = JSON.parseObject(params);
        Map map = JSON.parseObject(object.toString());

        int id = Integer.parseInt(map.get("id").toString());
        String eid = map.get("eid").toString();
        String name = map.get("name").toString();
        String address = map.get("address").toString();
        String longitude = map.get("longitude").toString();
        String latitude = map.get("latitude").toString();
        String etype = map.get("etype").toString();
        String summary = map.get("summary").toString();
        String report = map.get("report").toString();
        String username = map.get("username").toString();
        String userphone = map.get("userphone").toString();

        Travel travel = new Travel();
        travel.setId(id);
        travel.setEid(eid);
        travel.setName(name);
        travel.setAddress(address);
        travel.setLongitude(longitude);
        travel.setLatitude(latitude);
        travel.setEtype(etype);
        travel.setSummary(summary);
        travel.setReport(report);
        travel.setUsername(username);
        travel.setUserphone(userphone);

        return travelService.updateTravel(travel);
    }


    /*
     * 获取所有的民宿信息
     * */
    @ApiOperation(value="获取所有的民宿信息", notes="获取所有的民宿信息")
    @ResponseBody
    @RequestMapping(value = "/findsite", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public PageVo<TravelSite> findTravelSite(){
        return travelService.findTravelSite();
    }


    /*
     * 根据eid获取民宿信息
     * */
    @ApiOperation(value="根据eid获取民宿信息", notes="根据eid获取民宿信息")
    @ResponseBody
    @RequestMapping(value = "/findTravelSiteByEid", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResultVo findTravelSiteByEid(String eid){
        System.out.println(eid+"++++++++++");
        return travelService.findTravelSiteByEid(eid);
    }

    /*
     * 根据id获取民宿信息
     * */
    @ApiOperation(value="根据id获取民宿信息", notes="根据id获取民宿信息")
    @ResponseBody
    @RequestMapping(value = "/findTravelSiteById", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResultVo findTravelSiteById(Integer id){

        return travelService.findTravelSiteById(id);
    }


    /*
     * 保存抖音线路信息
     * */
    @ApiOperation(value="保存民宿信息", notes="保存民宿信息")
    @ResponseBody
    @RequestMapping(value = "/savesite", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResultVo saveTravelSite(@RequestBody String params){

        JSONObject object =JSON.parseObject(params);

        Map map = JSON.parseObject(object.toString());
        String eid = map.get("eid").toString();
        String name = map.get("name").toString();
        String address = map.get("address").toString();
        String longitude = map.get("longitude").toString();
        String latitude = map.get("latitude").toString();
        String etype = map.get("etype").toString();
        String summary = map.get("summary").toString();
        String report = map.get("report").toString();
        String username = map.get("username").toString();
        String userphone = map.get("userphone").toString();

        TravelSite douyin = new TravelSite();
        douyin.setEid(eid);
        douyin.setName(name);
        douyin.setAddress(address);
        douyin.setLongitude(longitude);
        douyin.setLatitude(latitude);
        douyin.setEtype(etype);
        douyin.setSummary(summary);
        douyin.setReport(report);
        douyin.setUsername(username);
        douyin.setUserphone(userphone);
        douyin.setFlag(1);

        return travelService.saveTravelSite(douyin);
    }

    /*
     * 修改民宿信息
     * */
    @ApiOperation(value="修改民宿信息", notes="修改民宿信息")
    @ResponseBody
    @RequestMapping(value = "/updatesite", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResultVo updateScenery(@RequestBody String params){
        JSONObject object =JSON.parseObject(params);
        Map map = JSON.parseObject(object.toString());

        int id = Integer.parseInt(map.get("id").toString());
        String eid = map.get("eid").toString();
        String name = map.get("name").toString();
        String address = map.get("address").toString();
        String longitude = map.get("longitude").toString();
        String latitude = map.get("latitude").toString();
        String etype = map.get("etype").toString();
        String summary = map.get("summary").toString();
        String report = map.get("report").toString();
        String username = map.get("username").toString();
        String userphone = map.get("userphone").toString();

        TravelSite douyin = new TravelSite();
        douyin.setId(id);
        douyin.setEid(eid);
        douyin.setName(name);
        douyin.setAddress(address);
        douyin.setLongitude(longitude);
        douyin.setLatitude(latitude);
        douyin.setEtype(etype);
        douyin.setSummary(summary);
        douyin.setReport(report);
        douyin.setUsername(username);
        douyin.setUserphone(userphone);

        return travelService.updateTravelSite(douyin);
    }

    @ApiOperation(value="民宿地点绑定", notes="民宿地点绑定")
    @ResponseBody
    @RequestMapping(value = "/binding", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResultVo binding(Integer aId, Integer tId) {
        ArticleTable010Mapping articleTable010Mapping = new ArticleTable010Mapping();
        articleTable010Mapping.setaId(aId);
        articleTable010Mapping.settId(tId);
        return travelService.savaMapping(articleTable010Mapping);
    }
}

